package com.jnu.myaccount.data.DataBank;

import android.content.Context;
import android.os.Environment;

import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.Money;
import com.jnu.myaccount.data.Person;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    private static Context context;
    private String listFileName="myList";
    private String MoneyFileName="moneyList";
    private String UserFileName="user";
    private ArrayList<Data> mList;


    public DataBank(Context contex) {
        this.context = contex;
    }

    public ArrayList<Data> loadData() {
        ArrayList<Data> myList = new ArrayList<>();
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(listFileName));
            myList = (ArrayList<Data>) objectInputStream.readObject();
            mList=myList;
            objectInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myList;
    }
    public void saveData(ArrayList<Data> myList) {
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(listFileName, Context.MODE_PRIVATE));
            objectOutputStream.writeObject(myList);
            objectOutputStream.close();
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void saveMoney(Money money){
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(MoneyFileName, Context.MODE_PRIVATE));
            objectOutputStream.writeObject(money);
            objectOutputStream.close();
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Money loadMoney(){
        ArrayList<Data> myList = new ArrayList<>();
        Money mMoney=null;
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(MoneyFileName));
            mMoney = (Money) objectInputStream.readObject();
            objectInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mMoney;
    }

    public Person loadUser(){
       Person person=null;
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(UserFileName));
            person = (Person) objectInputStream.readObject();
            objectInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return person;
    }

    public void saveUser(Person person){
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(UserFileName, Context.MODE_PRIVATE));
            objectOutputStream.writeObject(person);
            objectOutputStream.close();
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
